package com.atguigu.gmall.common.interceptor;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.util.UserAuthUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lhh
 * @ClassName UserAuthFeignInterceptor
 * @date 2022 2022/12/18
 */
@Slf4j
@Component
public class UserAuthFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        log.info("user信息 fegin拦截开始...");
        //获取请求
//        HttpServletRequest request = CartController.map
//        .get(Thread.currentThread());
//        HttpServletRequest request = CartController.threadLocal.get();

        //想要获取老请求 //获取之前的id
        HttpServletRequest request = UserAuthUtil.request();
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        if(!StringUtils.isEmpty(userId)){
            //userID
            requestTemplate.header(RedisConst.USER_ID_HEADER,userId);
        }
        //tempId
        String tempId = request.getHeader(RedisConst.TEMP_ID_HEADER);
        if (!StringUtils.isEmpty(tempId)){
            requestTemplate.header(RedisConst.TEMP_ID_HEADER,tempId);
        }
    }
}
